package com.toe.common.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class TimeUtil {

    /**
     * Data转换为LocalDateTime
     * @param time
     * @return
     */
    public static LocalDateTime dateToLocalDateTime(Date time) {
        return time.toInstant()
                .atZone(ZoneId.systemDefault())
                .toLocalDateTime();
    }

    /**
     * LocalDateTime转换为Data
     * @param time
     * @return
     */
    public static Date localDateTimeToDate(LocalDateTime time) {
        return Date.from(time.atZone(ZoneId.systemDefault()).toInstant());
    }

    /**
     * String转换成Date类型
     * @param str
     * @param type
     * @return
     * @throws ParseException
     */
    public static Date strToDate(String str, String type) throws ParseException {
        return str==null ? null : new SimpleDateFormat(type).parse(str);
    }

    /**
     * String转换成Date类型
     * @param str
     * @return
     * @throws ParseException
     */
    public static LocalDateTime strToLocalDateTime(String str) throws ParseException {
        return str==null ? null : dateToLocalDateTime(strToDate(str, "yyyy-MM-dd HH:mm:ss"));
    }

    /**
     * Date转换成String类型
     * @param data
     * @param formatType yyyy-MM-dd HH:mm:ss | yyyy年MM月dd日 HH时mm分ss秒
     * @return
     */
    public static String dateToString(Date data, String formatType) {
        return data==null ? null : new SimpleDateFormat(formatType).format(data);
    }

    /**
     * localDateTime转换成String类型
     * @param data
     * @param formatType
     * @return
     */
    public static String localDateTimeToString(LocalDateTime data, String formatType) {
        return data==null ? null : new SimpleDateFormat(formatType).format(localDateTimeToDate(data));
    }

    /**
     * 返回订单剩余支付时间(Date)
     *
     * @param time
     * @return
     */
    public static String getRemainingPayTime(Date time) {
        LocalDateTime createTime = TimeUtil.dateToLocalDateTime(time).plusMinutes(15);
        return getRemainingPayTime(createTime);
    }

    /**
     * 返回订单剩余支付时间(LocalDateTime)
     *
     * @param time
     * @return
     */
    public static String getRemainingPayTime(LocalDateTime time) {
        LocalDateTime nowTime = LocalDateTime.now();

        if (nowTime.isBefore(time)) {
            long min = ChronoUnit.MINUTES.between(nowTime, time);
            long sec = ChronoUnit.SECONDS.between(nowTime, time) - min * 60;
            String remainingPayTime = min + ":" + sec;
            System.out.println("===============剩余时间: " + remainingPayTime);
            return remainingPayTime;
        }

        return null;
    }

    /**
     * 获取年龄(Date)
     * @return
     */
    public static long getAge(Date birthday) {
        LocalDateTime time = TimeUtil.dateToLocalDateTime(birthday);
        return getAge(time);
    }

    /**
     * 获取年龄(LocalDateTime)
     * @return
     */
    public static long getAge(LocalDateTime birthday) {
        LocalDateTime nowTime = LocalDateTime.now();

        if (birthday.isBefore(nowTime)) {
            return ChronoUnit.YEARS.between(birthday, nowTime);
        }

        return 0L;
    }

    /**
     * 获取时期的方法(LocalDateTime)
     * @param time
     * @return
     */
    public static String getString(LocalDateTime time) {
        String data = time.toString().split("T")[0];

        return data.replace("-", "");
    }

}
